package cn.myspringboot.course.controller;

import cn.myspringboot.anno.PreAuthorize;
import cn.myspringboot.course.service.CourseService;
import cn.myspringboot.user.config.ExcelUtil;
import cn.myspringboot.user.domain.Course;
import cn.myspringboot.user.query.CourseQuery;
import cn.myspringboot.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@Api("课程相关接口")
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @PreAuthorize(name = "添加或修改课程信息",sn = "course:add",descs ="添加或修改课程信息")
    @ApiOperation("添加或修改课程信息")
    @PostMapping
    public R insert(@RequestBody Course course) {
        if(course.getId() == null){
            courseService.add(course);
        }else {
            courseService.update(course);
        }
        return R.ok();
    }
    @PreAuthorize(name = "根据id删除课程信息",sn = "course:delete",descs ="根据id删除课程信息")
    @ApiOperation("根据id删除课程信息")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id") Long id) {
        courseService.delete(id);
        return R.ok();
    }
    @PreAuthorize(name = "批量删除课程信息",sn = "course:delBatch",descs ="批量删除课程信息")
    @ApiOperation("批量删除课程信息")
    @PostMapping("/delBatch")
    public R delBatch(@RequestBody List<Long> ids) {
        courseService.batchDelete(ids);
        return R.ok();
    }
    @PreAuthorize(name = "根据id查询课程信息",sn = "course:getById",descs ="根据id查询课程信息")
    @ApiOperation("根据id查询课程信息")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(courseService.getById(id));
    }
    @PreAuthorize(name = "查询所有课程信息",sn = "course:getAll",descs ="查询所有课程信息")
    @ApiOperation("查询所有课程信息")
    @GetMapping("/getAll")
    public R getAll() {
        return R.ok(courseService.getAll());
    }
    @PreAuthorize(name = "分页查询课程信息",sn = "course:page",descs ="分页查询课程信息")
    @ApiOperation("分页查询课程信息")
    @PostMapping("/page")
    public R page(@RequestBody CourseQuery courseQuery) {
        PageHelper.startPage(courseQuery.getPage(),courseQuery.getSize());
        List<Course> courses = courseService.queryPage(courseQuery);
        PageInfo<Course> pageInfo = new PageInfo<>(courses);
        return R.ok(pageInfo);
    }
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws IOException {
        List<Course> all = courseService.getAll();
        ExcelUtil.exportExcel(all, "课程信息表", "博客教育管理系统-课程信息",Course.class,"Courses",response);
    }
    @PostMapping("/importExcel")
    public R importExcel(@RequestParam("file") MultipartFile file) throws IOException {
        List<Course> courses = ExcelUtil.importExcel(file, Course.class);
        courseService.batchSaveCourse(courses);
        return R.ok();
    }
}
